<?php
/**
 * Created by PhpStorm.
 * User: 尧
 * Date: 11/18/2016
 * Time: 2:07 PM
 */
namespace frontend\models;

use yii\base\Model;

class OrderForm extends Model{
    //模型类对象的属性，对应业务逻辑中需要操作的数据
    public $fetchTime;
    public $fetchAddress;
    public $shipTime;
    public $shipAddress;
    public $paymentMethod;
    public $note;
    public $isAgree;

    /**
     * 自定义的验证规则
     * @return array
     */
    public function rules(){
        return [
            //['fetchTime', 'required', 'message'=>'请选择取货时间段'],
            //与某个值必须严格相同
            //['isAgree', 'compare', 'compareValue'=>'1'],
            //与某个属性值要相等
            //['shipAddress', 'compare', 'compareAttribute'=>'fetchAddress'],
            //['shipTime', 'date'],
            //['shipTime', 'integer'], //整数
            //['shipTime', 'double'], //浮点数
           //['shipTime', 'number'], //所有数值
//            ['shipAddress', 'email'],
            //['shipAddress', 'email', 'checkDNS'=>true],
            //['shipAddress', 'match', 'pattern'=>'/^[a-zA-Z_][\w_]*/'],
            //不存在的验证器名字，则被视为自定义的验证规则
           // ['note', 'validateNode'],
//            ['note',function($data){
//
//            }],

            //固定默认值
            //['updated_at', 'default', 'value'=>'0'],
            ['shipAddress', 'filter', 'filter'=>'trim'],
            [['fetchTime', 'fetchAddress', 'shipTime', 'shipAddress',
            'paymentMethod', 'note', 'isAgree'], 'safe'],
        ];
    }

    /**
     * 自定义规则
     * @param $data
     */
    public function validateNode($data){

    }

    /**
     * @return bool|\common\models\Order
     */
    public function checkout(){
        if ($this->validate()) {
            //数据满足我们的验证规则
            //调用数据模型，将数据插入到数据表中
            $order = new \common\models\Order();
            $order->scenario = 'create';
            $order->attributes = $this->attributes;

            return $order->save() ? $order : false;
        }
    }


}